$(function () {
    var ysypColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {field: 'qcyp', title: '期初银票', rowspan: 2, type: '0', sftype: '0', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {title: '银票', colspan: 5, width: 100, align: 'right'},
            {field: 'qmyp', title: '期末银票', rowspan: 2, type: '0', sftype: '0', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ],
        [
            {field: 'ypbqzj', title: '本期增加', type: '0', sftype: '0', status: '1', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqtx', title: '本期贴现', type: '0', sftype: '0', status: '2', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqts', title: '本期托收', type: '0', sftype: '0', status: '3', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqbs', title: '本期背书', type: '0', sftype: '0', status: '4', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqdh', title: '本期兑换', type: '0', sftype: '0', status: '5', width: 15, align: 'right', formatter: formatSlForHtml2}
        ]
    ];

    var ysspColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {field: 'qcsp', title: '期初商票', rowspan: 2, type: '1', sftype: '0', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {title: '商票', colspan: 5, width: 100, align: 'right'} ,
            {field: 'qmsp', title: '期末商票', rowspan: 2, type: '1', sftype: '0', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ],
        [
            {field: 'spbqzj', title: '本期增加', type: '1', sftype: '0', status: '1', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqtx', title: '本期贴现', type: '1', sftype: '0', status: '2', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqts', title: '本期托收', type: '1', sftype: '0', status: '3', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqbs', title: '本期背书', type: '1', sftype: '0', status: '4', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqdh', title: '本期兑换', type: '1', sftype: '0', status: '5', width: 15, align: 'right', formatter: formatSlForHtml2}
        ]
    ];

    var ysColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {title: '期初', colspan: 3, width: 100, align: 'right'},
            {title: '银票', colspan: 5, width: 100, align: 'right'},
            {title: '商票', colspan: 5, width: 100, align: 'right'},
            {title: '期末', colspan: 3, width: 100, align: 'right'}
        ],
        [
            {field: 'qcyp', title: '期初银票', type: '0', sftype: '0', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qcsp', title: '期初商票', type: '1', sftype: '0', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qchj', title: '期初合计', type: '-1', sftype: '0', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqzj', title: '本期增加', type: '0', sftype: '0', status: '1', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqtx', title: '本期贴现', type: '0', sftype: '0', status: '2', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqts', title: '本期托收', type: '0', sftype: '0', status: '3', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqbs', title: '本期背书', type: '0', sftype: '0', status: '4', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqdh', title: '本期兑换', type: '0', sftype: '0', status: '5', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqzj', title: '本期增加', type: '1', sftype: '0', status: '1', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqtx', title: '本期贴现', type: '1', sftype: '0', status: '2', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqts', title: '本期托收', type: '1', sftype: '0', status: '3', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqbs', title: '本期背书', type: '1', sftype: '0', status: '4', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqdh', title: '本期兑换', type: '1', sftype: '0', status: '5', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmyp', title: '期末银票', type: '0', sftype: '0', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmsp', title: '期末商票', type: '1', sftype: '0', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmhj', title: '期末合计', type: '-1', sftype: '0', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ]
    ];

    var yfypColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {field: 'qcyp', title: '期初银票', rowspan: 2, type: '0', sftype: '1', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {title: '银票', colspan: 3, width: 100, align: 'right'},
            {field: 'qmyp', title: '期末银票', rowspan: 2, type: '0', sftype: '1', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ],
        [
            {field: 'ypbqzj', title: '本期增加', type: '0', sftype: '1', status: '6', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqcd', title: '本期承兑', type: '0', sftype: '1', status: '7', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqzf', title: '本期支付', type: '0', sftype: '1', status: '8', width: 15, align: 'right', formatter: formatSlForHtml2}
        ]
    ];

    var yfspColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {field: 'qcsp', title: '期初商票', rowspan: 2, type: '1', sftype: '1', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {title: '商票', colspan: 3, width: 100, align: 'right'},
            {field: 'qmsp', title: '期末商票', rowspan: 2, type: '1', sftype: '1', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ],
        [
            {field: 'spbqzj', title: '本期增加', type: '1', sftype: '1', status: '6', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqcd', title: '本期承兑', type: '1', sftype: '1', status: '7', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqzf', title: '本期支付', type: '1', sftype: '1', status: '8', width: 15, align: 'right', formatter: formatSlForHtml2},
        ]
    ];

    var yfColumns = [
        [
            {field: 'gsmc', title: '公司名称', rowspan: 2, width: 25, align: 'center'},
            {title: '期初', colspan: 3, width: 100, align: 'right'},
            {title: '银票', colspan: 3, width: 100, align: 'right'},
            {title: '商票', colspan: 3, width: 100, align: 'right'},
            {title: '期末', colspan: 3, width: 100, align: 'right'}
        ],
        [
            {field: 'qcyp', title: '期初银票', type: '0', sftype: '1', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qcsp', title: '期初商票', type: '1', sftype: '1', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qchj', title: '期初合计', type: '-1', sftype: '1', status: '0', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqzj', title: '本期增加', type: '0', sftype: '1', status: '6', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqcd', title: '本期承兑', type: '0', sftype: '1', status: '7', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'ypbqzf', title: '本期支付', type: '0', sftype: '1', status: '8', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqzj', title: '本期增加', type: '1', sftype: '1', status: '6', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqcd', title: '本期承兑', type: '1', sftype: '1', status: '7', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'spbqzf', title: '本期支付', type: '1', sftype: '1', status: '8', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmyp', title: '期末银票', type: '0', sftype: '1', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmsp', title: '期末商票', type: '1', sftype: '1', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2},
            {field: 'qmhj', title: '期末合计', type: '-1', sftype: '1', status: '9', width: 15, align: 'right', formatter: formatSlForHtml2}
        ]
    ];

    $("#pjkc_gsId").gsjgtree(1);

    var options = {
        url: $.webPath() + "/report/pjkc/pjkcList",
        toolbar: "#pjkc_toolbar",
        rownumbers: true,
        singleSelect: true,
        collapsible: true,
        fit: true,
        pageNumber: 1,
        fitColumns: true,
        showFooter: true,
        onClickCell: function (rowIndex, field, value) {
            var options = $("#grid_pjkc").datagrid('getColumnOption', field);
            var rows = $("#grid_pjkc").datagrid('getRows');
            var type = options.type;
            var sftype = options.sftype;
            var status = options.status;
            var gsid = rows[rowIndex].gsid;
            var title = options.title;
            if (status != 0 && status != 9) {
                var sf = (sftype == 0 ? "应收" : "应付");
                var ypsp = "票据";
                if (type == 0)  ypsp = "银票";
                if (type == 1)  ypsp = "商票";
                title = sf + ypsp + title;
            }
            if (field != 'gsmc')
                showDetail(title, $.webPath() + "/report/pjkc/pjkc_detail", gsid, type, sftype, status);
        }
    };

    var grid = null;

    var pjsflxType = $("[name='pjsflx_pjkc']").val();
    var pjlx = $("[name='pjlx_pjkc']").val();
    if (pjsflxType == 0 || !pjsflxType) {
        var col = ysColumns;
        if (pjlx == 'yp')
            col = ysypColumns;
        if (pjlx == 'sp')
            col = ysspColumns;
    } else {
        var col = yfColumns;
        if (pjlx == 'yp')
            col = yfypColumns;
        if (pjlx == 'sp')
            col = yfspColumns;
    }
    var queryParams = {
        year: $("#pjkc_year").val(),
        month: $("#pjkc_month").val(),
        pjsflx: $("[name='pjsflx_pjkc']").val(),
        gsId: $("#pjkc_gsId").combotree("getValue")
    };
    $.extend(options, {columns: col, queryParams: queryParams});
    grid = $("#grid_pjkc").datagrid(options);

//    //当类型切换时切换页面
//    $("[name='pjsflx_pjkc']").combobox({onSelect: function () {
//        //先清空表格
//        var pjsflxType = $("[name='pjsflx_pjkc']").val();
//        var pjlx = $("[name='pjlx_pjkc']").val();
//        if (pjsflxType == 0 || !pjsflxType) {
//            var col = ysColumns;
//            if (pjlx == 'yp')
//                col = ysypColumns;
//            if (pjlx == 'sp')
//                col = ysspColumns;
//            $.extend(options, {columns: col});
//            grid = $("#grid_pjkc").datagrid(options);
//            pjkc_search();
//        } else {
//            var col = yfColumns;
//            if (pjlx == 'yp')
//                col = yfypColumns;
//            if (pjlx == 'sp')
//                col = yfspColumns;
//            $.extend(options, {columns: col});
//            grid = $("#grid_pjkc").datagrid(options);
//            pjkc_search();
//        }
//    }});
//
//    //当类型切换时切换页面
//    $("[name='pjlx_pjkc']").combobox({onSelect: function () {
//        //先清空表格
//        var pjsflxType = $("[name='pjsflx_pjkc']").val();
//        var pjlx = $("[name='pjlx_pjkc']").val();
//        if (pjsflxType == 0 || !pjsflxType) {
//            var col = ysColumns;
//            if (pjlx == 'yp')
//                col = ysypColumns;
//            if (pjlx == 'sp')
//                col = ysspColumns;
//            $.extend(options, {columns: col});
//            grid = $("#grid_pjkc").datagrid(options);
//            pjkc_search();
//        } else {
//            var col = yfColumns;
//            if (pjlx == 'yp')
//                col = yfypColumns;
//            if (pjlx == 'sp')
//                col = yfspColumns;
//            $.extend(options, {columns: col});
//            grid = $("#grid_pjkc").datagrid(options);
//            pjkc_search();
//        }
//    }});


    //查询按钮
    $("#btn_pjkc_search").click(function () {
        var pjsflxType = $("[name='pjsflx_pjkc']").val();
        var pjlx = $("[name='pjlx_pjkc']").val();
        if (pjsflxType == 0 || !pjsflxType) {
            var col = ysColumns;
            if (pjlx == 'yp')
                col = ysypColumns;
            if (pjlx == 'sp')
                col = ysspColumns;
        } else {
            var col = yfColumns;
            if (pjlx == 'yp')
                col = yfypColumns;
            if (pjlx == 'sp')
                col = yfspColumns;
        }
        var queryParams = {
            year: $("#pjkc_year").val(),
            month: $("#pjkc_month").val(),
            pjsflx: $("[name='pjsflx_pjkc']").val(),
            gsId: $("#pjkc_gsId").combotree("getValue")
        };
        $.extend(options, {columns: col, queryParams: queryParams});
        grid = $("#grid_pjkc").datagrid(options);
    });

    //查询票据
    function pjkc_search() {
        $('#grid_pjkc').datagrid('reload',
            {
                pjsflx: $("[name='pjsflx_pjkc']").val(),
                year: $("[name='year']").val(),
                month: $("#pjkc_month").val(),
                gsId: $("#pjkc_gsId").combotree("getValue")
            }
        );
    }

    var showDetail = function (title, url, gsid, type, sftype, status) {
        var win = $.window({
            title: title,
            width: 800,
            height: 500,
            maximizable: true,
            cache: false,
            iconCls: 'icon-tip',
            url: url + "?gsId=" + gsid + "&type=" + type + "&sftype=" + sftype + "&status=" + status +
                "&year=" + $("#pjkc_toolbar input[name='year']").val() + "&month=" + $("#pjkc_toolbar input[name='month']").val(),
            modal: true,
            buttons: [],
            onMaximize: function () {
                $(this).find("#pjkc_detail_grid").edatagrid("resize");
            },
            onRestore: function () {
                $(this).find("#pjkc_detail_grid").edatagrid("resize");
            }
        });
    }
});

